package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.StrategyCommendQuery;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.domain.StrategyTemplate;
import cn.wolfcode.luowowo.search.query.StatisVoQuery;
import cn.wolfcode.luowowo.search.service.IStrategySearchService;
import cn.wolfcode.luowowo.search.vo.StatisVo;
import cn.wolfcode.luowowo.website.annoation.LoginCheck;
import cn.wolfcode.luowowo.website.annoation.UserParam;
import cn.wolfcoe.luowowo.common.util.baiducheck.SensitiveWordsUtil;
import cn.wolfcoe.luowowo.common.util.AjaxResult;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("strategy")
public class StrategyController {
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyTagService strategyTagService;
    @Reference
    private IStrategyStatisVOService strategyStatisVOService;
    @Reference
    private IStrategyCommentService strategyCommentService;
    @Reference
    IStrategyCommendService strategyCommendService;
    @Reference
    private IStrategySearchService strategySearchService;


    @RequestMapping("")
    public String index(Model model, StrategyCommendQuery qo) {
        List <StrategyCommend> commends = strategyCommendService.queryStrategyCommentTop5( qo );
        model.addAttribute( "commends", commends );
        //reids的排行,热门,国外,国内
        List <StrategyStatisVO> hotCds = strategyStatisVOService.queryHotStrategyTop10();
        model.addAttribute( "hotCds", hotCds);
        //获取所有国内外的排行list
        List <StrategyStatisVO> vos = strategyStatisVOService.queryStrategyCommendVO();
        List <StrategyStatisVO> unabroadCds = new ArrayList <>();
        List <StrategyStatisVO> abroadCds = new ArrayList <>();
        for (StrategyStatisVO vo : vos) {
            if (vo.isIsabroad() && abroadCds.size() < 10) {
                abroadCds.add( vo );
            } else {
                if (unabroadCds.size() < 10) {
                    unabroadCds.add( vo );
                }
            }
        }
        model.addAttribute( "unabroadCds", unabroadCds );
        model.addAttribute( "abroadCds", abroadCds );

        List <Map <String, Object>> themeCds = strategySearchService.queryTemeCds();
        model.addAttribute( "themeCds", themeCds );
        //es的分组查询,主题,国内,国外的分组
        //chinas
        List <StatisVo> chinas = strategySearchService.queryChinasGroub();
        model.addAttribute( "chinas", chinas );
        //按照国家分类,查询省份的id,名称,及其下面的攻略的数量
        //abroads
        List <StatisVo> abroads = strategySearchService.queryAbroadsGroub();
        model.addAttribute( "abroads", abroads );
        //themes
        List <StatisVo> themes = strategySearchService.queryThemesGroubTop10();
        model.addAttribute( "themes", themes );
        return "strategy/index";
    }

    @RequestMapping("/detail")
    public String detail(Long id, Model model, @UserParam UserInfo userInfo) {
        StrategyDetail detail = strategyDetailService.get( id );
        StrategyContent strategyContent = strategyDetailService.queryContent( id );
        detail.setStrategyContent( strategyContent );
        model.addAttribute( "detail", detail );
        //阅读数加1
        strategyStatisVOService.viewnumIncrease( id, 1 );
        //热点排行数+1
        String zetKey = RedisKeys.STRATEGY_STATIS_ZSET_HOTTOP.getPrifx();
        strategyStatisVOService.addStrategyDetailTopScore( zetKey, id, 1 );
        //当前登录用户是否收藏该攻略
        if (userInfo != null) {
            Boolean isFavor = strategyStatisVOService.checkFavor( id, userInfo.getId() );
            model.addAttribute( "isFavor", isFavor );
        }
        model.addAttribute( "vo", strategyStatisVOService.getStrategyStatisVO( id ) );

        return "strategy/detail";
    }

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQuery qo) {
        //攻略
        List <Destination> toasts = destinationService.getToasts( qo.getDestId() );
        Destination destination = toasts.remove( toasts.size() - 1 );
        model.addAttribute( "toasts", toasts );
        model.addAttribute( "dest", destination );

        //当前标签
        List <StrategyTag> tags = strategyTagService.list();
        model.addAttribute( "tags", tags );
        //当前目的地下的游记
        PageInfo pageInfo = strategyDetailService.query( qo );
        model.addAttribute( "pageInfo", pageInfo );
        return "strategy/list";
    }


    @RequestMapping("/commentAdd")
    @LoginCheck
    @ResponseBody
    public AjaxResult save(Model model, StrategyComment comment, @UserParam UserInfo userInfo) {

        comment.setUserId( userInfo.getId() );
        comment.setUsername( userInfo.getNickname() );
        comment.setCity( userInfo.getCity() );
        comment.setLevel( userInfo.getLevel() );
        comment.setHeadUrl( userInfo.getHeadImgUrl() );
        comment.setContent( SensitiveWordsUtil.SensitiveWordCheck( comment.getContent() ).getContent() );
        strategyCommentService.save( comment );
        strategyStatisVOService.replyNumIncrease( comment.getDetailId(), 1 );
        StrategyStatisVO vo = strategyStatisVOService.getStrategyStatisVO( comment.getDetailId() );
        //增加排行的分数
        String zetKey = RedisKeys.STRATEGY_STATIS_ZSET_HOTTOP.getPrifx();
        strategyStatisVOService.addStrategyDetailTopScore( zetKey, comment.getDetailId(), 1 );
        model.addAttribute( "vo", vo );
        return new AjaxResult( true, "添加成功" ).addData( vo.getReplynum() );
    }

    @RequestMapping("/comment")
    public Object comment(Model model, @ModelAttribute("qo") StrategyCommentQuery qo) {
        Page <StrategyComment> page = strategyCommentService.query( qo );
        model.addAttribute( "page", page );
        return "strategy/commentTpl";
    }

    @RequestMapping("/commentThumbUp")
    @ResponseBody
    @LoginCheck
    public Object commentThumbUp(Model model, String toid, Long fromid) {
        strategyCommentService.commentThumbUp( toid, fromid );
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Model model, Long sid, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            AjaxResult result = new AjaxResult( false, "请先登录" );
            result.setCode( 102 );
            return result;
        }
        Boolean flag = strategyStatisVOService.favor( sid, userInfo );
        String zetKey = RedisKeys.STRATEGY_STATIS_ZSET_COMMEND.getPrifx();
        //如果是收藏,排名分数加1,如果是取消收藏,排名分数减1
        strategyStatisVOService.addStrategyDetailTopScore( zetKey, sid, flag ? 1 : -1 );
        return new AjaxResult( flag ).addData( strategyStatisVOService.getStrategyStatisVO( sid ) );
    }

    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Model model, Long sid, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            AjaxResult result = new AjaxResult( false, "请先登录" );
            result.setCode( 102 );
            return result;
        }
        Boolean flag = strategyStatisVOService.strategyThumbup( sid, userInfo.getId() );
        //如果是已经顶过,不添加数据,如果没有顶过,则添加数据
        if (flag) {
            String zetKey = RedisKeys.STRATEGY_STATIS_ZSET_COMMEND.getPrifx();
            strategyStatisVOService.addStrategyDetailTopScore( zetKey, sid, 1 );
        }

        return new AjaxResult( flag ).addData( strategyStatisVOService.getStrategyStatisVO( sid ) );
    }

    @RequestMapping("/searchPage")
    public String searchPage(Model model,@ModelAttribute("qo") StatisVoQuery qo){
     Page<StrategyTemplate> page=  strategySearchService.query(qo);
        model.addAttribute( "page", page );
        return "strategy/searchPageTpl";
    }

    //转发接口
    @RequestMapping("/strategyShare")
    @ResponseBody
    public Object strategyShare(Model model, Long sid, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            AjaxResult result = new AjaxResult( false, "请先登录" );
            result.setCode( 102 );
            return result;
        }
        Boolean flag = strategyStatisVOService.strategyShare( sid, userInfo.getId() );
        return new AjaxResult( flag ).addData( strategyStatisVOService.getStrategyStatisVO(sid) );
    }
}
